package com.example.demo1.inquiry.controller;


import com.example.demo1.inquiry.model.InquiryTotal;
import com.example.demo1.inquiry.model.vo.InquiryAddVo;
import com.example.demo1.inquiry.model.vo.InquiryQueryVo;
import com.example.demo1.inquiry.model.vo.InquiryTotalVo;
import com.example.demo1.inquiry.model.vo.InquiryUpdateVo;
import com.example.demo1.inquiry.service.InquiryService;
import com.example.demo1.wyy.util.AllRecords;
import com.example.demo1.wyy.util.Result;
import com.example.demo1.wyy.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Lx on 2018/6/27.
 */
@RestController
public class InquiryController {


    @Autowired
    private InquiryService inquiryService;

    @PostMapping("inquiry/add")
    public Result<InquiryAddVo> addInquiry(@RequestBody @Valid InquiryAddVo inquiryAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return ResultVO.error(1,bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.addInquiry(inquiryAddVo);
    }

    @PostMapping("inquiry/update")
    public Result<InquiryUpdateVo> updateInquiry(@RequestBody @Valid InquiryUpdateVo updateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return ResultVO.error(1,bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.updateInquiry(updateVo);
    }
    @PostMapping("inquiry/queryPage")
    public Result<AllRecords<InquiryTotal>> queryPage(@RequestBody InquiryQueryVo inquiryQueryVo){
        return inquiryService.queryPage(inquiryQueryVo);
    }

    @GetMapping("inquiry/seeInquiry")
    public Result<InquiryTotalVo> seeInquiry(@RequestParam("id") Integer id){
        return inquiryService.seeInquiry(id);
    }

}
